//
//  MineDisplayPhotoCollectionViewCell.swift
//  MT
//
//  Created by M on 2019/12/27.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import MTCategory
import Kingfisher

/// MinePhotoCollectionDisplayViewController mainCollectionView 的 Cell
class MineDisplayPhotoCollectionViewCell: UICollectionViewCell {
    
    var model: MinePhotoCollectionDisplayModel? {
        didSet {
            guard let coverImage = model?.coverImg else {
                if let href = model?.href {
                    coverImageView.kf.setImage(with: URL(string: href), placeholder: UIImage(named: defaultPlaceholderIcon60x60))
                }
                return
            }
            coverImageView.kf.setImage(with: URL(string: coverImage), placeholder: UIImage(named: defaultPlaceholderIcon60x60))
        }
    }
    
    /// 封面图
    lazy var coverImageView: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFill
        imgView.clipsToBounds = true
        imgView.layer.cornerRadius = 10
        imgView.tag = 9000
        return imgView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initializeUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: ------------------ Overrides ----------------------
extension MineDisplayPhotoCollectionViewCell {
    
    override func prepareForReuse() {
        coverImageView.image = nil
    }
    
}

// MARK: ------------------ 初始化 ----------------------
extension MineDisplayPhotoCollectionViewCell {
    
    private func initializeUI() {
        contentView.backgroundColor = UIColor(hex: 0x161616)
        contentView.addSubview(coverImageView)
        coverImageView.snp.makeConstraints { (make) in
            make.top.equalTo(contentView).offset(15)
            make.left.equalTo(contentView).offset(15)
            make.right.equalTo(contentView).offset(-15)
            make.bottom.equalTo(contentView).offset(-5)
        }
    }
    
}

/// MinePhotoCollectionDisplayViewController Collection 的 Cell
class MineDisplayCoverPhotoCollectionViewCell: UICollectionViewCell {

    /// 媒体
    var model: MinePhotoCollectionDisplayModel? {
        didSet {
            guard let model = model else { return }
            guard let coverImage = model.coverImg else {
                if let href = model.href {
                    imgView.kf.setImage(with: URL(string: href))
                }
                imgView.layer.borderWidth = model.isDisplaying ? 1 : 0
                return
            }
            imgView.kf.setImage(with: URL(string: coverImage))
            imgView.layer.borderWidth = model.isDisplaying ? 1 : 0
        }
    }
    
    /// 图片
    lazy var imgView: UIImageView = {
        let imgView = UIImageView(frame: self.contentView.bounds)
        imgView.contentMode = .scaleAspectFill
        imgView.layer.cornerRadius = 4
        imgView.clipsToBounds = true
        imgView.layer.borderColor = UIColor.theme.main.cgColor
        return imgView
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        initializeUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: ------------------ Overrides ----------------------
extension MineDisplayCoverPhotoCollectionViewCell {
    
    override func prepareForReuse() {
        imgView.image = nil
    }
    
}

// MARK: ------------------ 初始化 ----------------------
extension MineDisplayCoverPhotoCollectionViewCell {
    
    private func initializeUI() {
        contentView.addSubview(imgView)
//        imgView.snp.makeConstraints { (make) in
//            make.edges.equalTo(contentView)
//        }
    }
    
}
